//
//  JFBSettlementDetailsDS.m
//  ios-supplier
//
//  Created by Benlai on 15/8/30.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBSettlementDetailsDS.h"

@implementation JFBSettlementDetailsDS

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.so_number = [[attributes objectForKey: @"so_number"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey:@"so_number"];
    self.balance_id = [[attributes objectForKey: @"balance_id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"balance_id"]  integerValue];
    self.total_price = [[attributes objectForKey: @"total_price"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"total_price"]];
    self.instead_price = [[attributes objectForKey: @"instead_price"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"instead_price"]];
    self.settle_price = [[attributes objectForKey: @"settle_price"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"settle_price"]];
    self.freight = [[attributes objectForKey: @"freight"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"freight"]];
    self.so_bonus = [[attributes objectForKey: @"so_bonus"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"so_bonus"];
    self.so_fine = [[attributes objectForKey: @"so_fine"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"so_fine"];
    self.transfer_bonus = [[attributes objectForKey: @"transfer_bonus"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"transfer_bonus"];
//    self.long_distribution = [[attributes objectForKey: @"long_distribution"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"long_distribution"];
    
    
    return self;
}

+ (NSURLSessionDataTask *)loadSettlementDetail:(void (^)(NSArray *statisticsList, NSError *error))block balanceID:(NSUInteger)balance_id{
    NSDictionary *parameters = @{@"balance_id": [NSNumber numberWithInteger:balance_id]};
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/balance/list/so.json" parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSLog(@"day payment:%@", JSON);
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            
            for( NSDictionary *item in response ) {
                [result addObject:[[JFBSettlementDetailsDS alloc] initWithAttributes:item]];
            }
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    return nil;
}
@end
